/**
 * 
 */
package com.erbaoshen.util;

import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Random;

/**
 * @author w.weizhen
 *
 */
public class ErBaoSystemUtiil {
	/**
	 * 获取本机mac码
	 * @return
	 */
	public static String MACAddress() {
		try {
			Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
			byte[] mac = null;
			while (allNetInterfaces.hasMoreElements()) {
				NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
				if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
					continue;
				} else {
					mac = netInterface.getHardwareAddress();
					if (mac != null) {
						StringBuilder sb = new StringBuilder();
						for (int i = 0; i < mac.length; i++) {
							sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
						}
						if (sb.length() > 0) {
							return sb.toString();
						}
					}
				}
			}
		} catch (Exception e) {
			// _logger.error("MAC地址获取失败", e);
			e.printStackTrace();
		}
		return "";
	}
	
	
	/**
	 * 生成随机字符串
	 * @param leng
	 * @return
	 */
	public static String reandomStr(int len) {
		String reandstr = "";
		/**
		 * 基
		 */
		String basestr = "ABCDEFGHIGKLMNOPQRSTUVWXYZ0123456789";
		/**
		 * 创建随机数生成器
		 */
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		/**
		 * 循环生成随机字符串
		 */
		for (int i = 0; i < len; i++) {
			int number = random.nextInt(len);
			sb.append(basestr.charAt(number));
		}
		reandstr = sb.toString();
		return reandstr;
	}
	
}
